#!/dis/sh
# descr: given a a.out(10) file boot it using boot(3)
load std

fn tohex(){ mdb /dev/null $1'=X'; }
fn toint(){ echo $1 | calc; }
fn _magic(){ b=$1; tohex `{echo '((((4 * '$b' ) + 0 ) * '$b' ) + 7)' | calc}; }

fn tole(){
	v=$1; b=(`{echo $v | sed 's/^0x//; s/(..)/\1 /g'})
	echo 0x${index 4 $b}^${index 3 $b}^${index 2 $b}^${index 1 $b}
}

file=$1
if{~ $#file 0 || ! ls $file}{
	echo 'usage: kexec file'
	exit
}

hdr=`{mdb $file '0x0, 8 /X'}

magic=`{tole ${index 1 $hdr}}
text=`{tole ${index 2 $hdr}}
data=`{tole ${index 3 $hdr}}
bss=`{tole ${index 4 $hdr}}
entry=`{tole ${index 6 $hdr}}

if{! ~ $magic `{_magic 20}}{
	echo bad magic $magic
	exit
}

#echo a.out hdr: $hdr
echo entry $entry
echo text $text
echo data $data
echo bss $bss

#echo watchdog off > /dev/sheevactl
dd -if $file -of /dev/sheevamem -oseek
echo dd -if $file -of /dev/sheevamem -oseek `{echo $entry / 512 | calc}
echo mdb -wo /dev/boot '0/W '`{tole $entry}
